import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TrafficSystem {
    
    private static final int CAR_THRESHOLD = 5;
    
    public char WNWwalk, WNEwalk, WSWwalk, WSEwalk;
    public char ENWwalk, ENEwalk, ESWwalk, ESEwalk;
    public String WNcars, WWcars, WEcars, WScars;
    public String ENcars, EWcars, EEcars, EScars;
    public String WLightDirection, ELightDirection;
    public boolean backup = false;
    public boolean emergency = false;
    public boolean WEWwalk = false;
    public boolean WNSwalk = false;
    public boolean EEWwalk = false;
    public boolean ENSwalk = false;
    public int[] emergencyLocation = { -1, -1 };
    public String emergencyArea = "off-street";

    public TrafficSystem(String filename) {
        try {
            BufferedReader file = new BufferedReader(new FileReader(filename));
            String currentLine;
            int lineNumber = 1;
            while ((currentLine = file.readLine()) != null) {
                try {
                    if (currentLine.length() != 40) {
                        System.out.println("There is an error in the input file");
                        System.exit(1);
                    }
                    if (currentLine.contains("!")) {
                        if (emergency == true) {
                            System.out.println("There cannot be two emergencies");
                            System.exit(1);
                        }
                        int column = currentLine.indexOf('!');
                        emergency = true;
                        emergencyLocation[0] = lineNumber;
                        emergencyLocation[1] = column;
                    }
                    System.out.println(currentLine);
                    if (lineNumber == 3) {
                        WNWwalk = currentLine.charAt(7);
                        if (Integer.parseInt(currentLine.charAt(12) + "") > CAR_THRESHOLD)
                            WNcars = "wn-heavy";
                        else
                            WNcars = "wn-light";
                        WNEwalk = currentLine.charAt(16);
                        ENWwalk = currentLine.charAt(23);
                        if (Integer.parseInt(currentLine.charAt(28) + "") > CAR_THRESHOLD)
                            ENcars = "en-heavy";
                        else
                            ENcars = "en-light";
                        ENEwalk = currentLine.charAt(32);
                    }
                    if (lineNumber == 5) {
                        if (currentLine.charAt(12) == '^')
                            WLightDirection = "NS";
                        else
                            WLightDirection = "EW";
                        if (currentLine.charAt(28) == '^')
                            ELightDirection = "NS";
                        else
                            ELightDirection = "EW";
                    }
                    if (lineNumber == 6) {
                        if (Integer.parseInt(currentLine.charAt(7) + "") > CAR_THRESHOLD)
                            WWcars = "ww-heavy";
                        else
                            WWcars = "ww-light";
                        if (Integer.parseInt(currentLine.charAt(16) + "") > CAR_THRESHOLD)
                            WEcars = "we-heavy";
                        else
                            WEcars = "we-light";
                        if (Integer.parseInt(currentLine.charAt(23) + "") > CAR_THRESHOLD)
                            EWcars = "ew-heavy";
                        else
                            EWcars = "ew-light";
                        if (Integer.parseInt(currentLine.charAt(32) + "") > CAR_THRESHOLD)
                            EEcars = "ee-heavy";
                        else
                            EEcars = "ee-light";
                    }
                    if (lineNumber == 9) {
                        WSWwalk = currentLine.charAt(7);
                        if (Integer.parseInt(currentLine.charAt(12) + "") > CAR_THRESHOLD)
                            WScars = "ws-heavy";
                        else
                            WScars = "ws-light";
                        WSEwalk = currentLine.charAt(16);
                        ESWwalk = currentLine.charAt(23);
                        if (Integer.parseInt(currentLine.charAt(28) + "") > CAR_THRESHOLD)
                            EScars = "es-heavy";
                        else
                            EScars = "es-light";
                        ESEwalk = currentLine.charAt(32);
                    }

                    lineNumber++;
                } catch (NumberFormatException n) {
                    n.printStackTrace();
                }

            }
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (WNWwalk == 'N' || WNEwalk == 'N' || WSWwalk == 'N' || WSEwalk == 'N')
            WNSwalk = true;
        if (WNWwalk == 'E' || WNEwalk == 'E' || WSWwalk == 'E' || WSEwalk == 'E')
            WEWwalk = true;
        if (ENWwalk == 'N' || ENEwalk == 'N' || ESWwalk == 'N' || ESEwalk == 'N')
            WNSwalk = true;
        if (ENWwalk == 'E' || ENEwalk == 'E' || ESWwalk == 'E' || ESEwalk == 'E')
            EEWwalk = true;

        if (emergencyLocation[0] != -1) {
            int row = emergencyLocation[0];
            int column = emergencyLocation[1];
            if (row <= 4 && column > 8) {
                if (column < 15)
                    emergencyArea = "WN";
                else if (column > 24 && column < 31)
                    emergencyArea = "EN";
            } else if (row <= 7) {
                emergencyArea = "WW";
                if (column > 8 && column < 15)
                    emergencyArea = "W";
                else if (column >= 15 && column < 24)
                    emergencyArea = "M";
                else if (column >= 24 && column < 31)
                    emergencyArea = "E";
                else if (column > 30)
                    emergencyArea = "EE";
            } else if (row > 7 && column > 8) {
                if (column < 15)
                    emergencyArea = "WS";
                else if (column > 24 && column < 31)
                    emergencyArea = "ES";
            }
        }
    }
}
